package com.keai.service;


import com.alibaba.dubbo.config.annotation.Service;
import com.keai.constant.MessageConst;
import com.keai.dao.MemberDao;
import com.keai.dao.OrderDao;
import com.keai.dao.OrderSettingDao;
import com.keai.entity.Result;
import com.keai.pojo.Member;
import com.keai.pojo.Order;
import com.keai.pojo.OrderSetting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    OrderSettingDao orderSettingDao;

    @Autowired
    MemberDao memberDao;


    @Autowired
    OrderDao orderDao;



    @Override
    @Transactional
    public Result addOrder(Map<String, Object> map) throws Exception {
        //获取用户选择的日期
        String orderDatestr = String.valueOf(map.get("orderDate"));
        SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
        Date orderDate = dateFormat.parse(orderDatestr);
        //获取用户的手机号
        String telephone = String.valueOf(map.get("telephone"));
        //获取用户姓名
        String name = String.valueOf(map.get("name"));
        //获取用户性别
        String sex = String.valueOf(map.get("sex"));
        //获取用户身份证号码
        String idCard = String.valueOf(map.get("idCard"));
        //获取套餐id
        Integer setmealId = Integer.parseInt(String.valueOf(map.get("setmealId")));
        //获取预约类型
        String orderType = String.valueOf(map.get("orderType"));

        //1.判断这日期是否进行了预约设置
        OrderSetting orderSetting=orderSettingDao.findByDate(orderDate);

        if (orderSetting==null){
            return new Result(false, MessageConst.GET_ORDERSETTING_FAIL);
        }
        //2.该日期是否预约满了
        if(orderSetting.getReservations()>=orderSetting.getNumber()){
            return new Result(false,MessageConst.GET_ORDERSETTING_FAIL);
        }
        //3.判断该用户是否是会员
        Member member = memberDao.findByTelephone(telephone);
        if (member == null){
            //不是会员自动注册为会员(注册会员,主键回显)
            //创建会员对象
            member=new Member();
            //id回显
            //name:姓名
            member.setName(name);
            //sex:性别
            member.setSex(sex);
            //idCard:身份证账号
            member.setIdCard(idCard);
            //phoneNumber:手机号码
            member.setPhoneNumber(telephone);
            //regTime:注册时间(当前时间)
            member.setRegTime(new Date());
            //自动注册会员
            memberDao.add(member);
        }else {
            //判断该套餐是否被重复预约
            //条件：该会员（会员id），该日期（orderDate），该套餐（套餐id）
            Order order=new Order();
            order.setSetmealId(setmealId);
            order.setOrderDate(orderDate);
            order.setMemberId(member.getId());
            long count= orderDao.getByOrder(order);
             if(count>0){
               return new Result(false,MessageConst.HAS_ORDERED);
           }

        }
        //5添加预约信息:访问dao,开始预约
        Order order = new Order();
        //id自动生成
        //member_id : 会员id
        order.setMemberId(member.getId());
        //orderDate：预约日期
        order.setOrderDate(orderDate);
        //orderType:预约类型
        order.setOrderType(orderType);
        //orderStatus： 已到诊/未到诊
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        //setmeal_id: 套餐id
        order.setSetmealId(setmealId);
        //添加预约对象
        orderDao.addOrder(order);
        //预约完成了
        //6. 预约完成后，更新已预约人数
        orderSetting.setReservations(orderSetting.getReservations()+1);
        orderSettingDao.edit(orderSetting);
        //返回预约成功
        return new Result(true,MessageConst.ORDER_SUCCESS, order);

    }

    @Override
    public Map<String, Object> findById(Integer id) {
        return orderDao.findById(id);
    }


}
